xquery version "1.0-ml";
module namespace my="my";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
import module namespace ut="http://marklogic.com/test/unit" at "lib/unit-test.xqy";
import module namespace util="http://marklogic.com/util/sequence" at "../lib/sequence.xqy";
declare namespace error="http://marklogic.com/xdmp/error";

declare function my:get-tests() as xs:QName* {
	for $test in (
		"test-reduce-sum",
		"test-reduce-empty",
		"test-map",
		"test-map-empty",
		"test-map-elements"
	) return QName("my", $test)
};

declare private function my:sum($a, $b) as item() {
	$a + $b
};

declare private function my:add($a) as item() {
	$a + 100
};

declare function my:test-reduce-sum() {
	let $result := util:reduce((1,2,3,4), 0, xdmp:function(xs:QName("my:sum")))
	return (
		ut:assert-equals(10, $result)
	)
};

declare function my:test-reduce-empty() {
	let $result := util:reduce((), 0, xdmp:function(xs:QName("my:sum")))
	return (
		ut:assert-empty($result)
	)
};

declare function my:test-map() {
	let $result := util:map((1, 2, 3, 4), xdmp:function(xs:QName("my:add")))
	return (
		ut:assert-equals(4, count($result)),
		ut:assert-equals((101, 102, 103, 104), $result)
	)
};

declare function my:test-map-empty() {
	let $result := util:map((), xdmp:function(xs:QName("my:add")))
	return (
		ut:assert-empty($result)
	)
};

declare function my:transform($el as element()) as element() {
	<WRAPPER>{$el}</WRAPPER>
};

declare function my:test-map-elements() {
	let $input := (<a>A</a>, <b>B</b>, <c>C</c>, <d>D</d>, <e>E</e>)
	let $result := util:map($input, xdmp:function(xs:QName("my:transform")))
	return (
		ut:assert-equals(5, count($result/self::WRAPPER/(a|b|c|d|e)))
	)
};